﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Media.Imaging;

namespace MyPoints.UserControls.Converters
{
  public class ImageStringToImageConverter : IValueConverter
  {
    public object Convert(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
      var ret = _Convert(value as wptImageType);
      return ret;
    }

    public BitmapSource _Convert(wptImageType value)
    {
      Uri uri;
      BitmapImage ret;
      if (value == null)
      {
        uri = new Uri(@"/Imgs/emptyGallery.png", UriKind.Relative);
        ret = new BitmapImage(uri);
      }
      else
      {
        switch (value.type)
        {
          case wptImageTypeType.File:
          case wptImageTypeType.Url:
            uri = new Uri(value.Value);
            ret = new BitmapImage(uri);
            break;
          case wptImageTypeType.Embedded:
            ret = Shared.StringToBitmapImage(value.Value);
            break;
          case wptImageTypeType.Folder:
            throw new NotImplementedException();
          default:
            throw new NotSupportedException();
        }
      }

      return ret;
    }

    public object ConvertBack(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
      throw new NotSupportedException();
    }
  }
}
